﻿<%--
Copyright (C) 2004-2007  Autodesk, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of version 2.1 of the GNU Lesser
General Public License as published by the Free Software Foundation.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
--%>

<%@ Page language="c#" debug="true"%>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Collections.Specialized" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Globalization" %>
<%@ Import Namespace="OSGeo.MapGuide" %>
<%@ Import Namespace="Jayrock.Json" %>
<%@ Import Namespace="Jayrock.Json.Conversion" %>

<!-- #Include File="classes/markupmanager.aspx -->
<!-- #Include File="classes/markupcommand.aspx -->

<script runat="server">
String errorMsg = null;
String errorDetail = null;
NameValueCollection args = null;
MarkupManager markupManager = null;
Hashtable availableMarkup = null;
Hashtable openMarkup = null;
bool refreshMap = false;
String layoutId = null;
</script>

<%
    InitializeWebTier();

    args = GetRequestParameters();
    markupManager = new MarkupManager(args);

    try
    {
        UnSuccessText.Visible = false;
        String markupCommand = GetParameter(args, "MARKUPCOMMAND");
        if (markupCommand != null && markupCommand.Length > 0)
        {
            int cmd = Int32.Parse(markupCommand);
            switch (cmd)
            {
                case MarkupCommand.None:
                    break;
                case MarkupCommand.Create:
				    markupManager.CreateMarkup();
				    refreshMap = true;
				    break;
			    case MarkupCommand.Open:
				    markupManager.OpenMarkup();
				    refreshMap = true;
				    break;
			    case MarkupCommand.Delete:
                    if (!markupManager.DeleteMarkup())
                    {
                        UnSuccessText.Visible = true;
                        UnSuccessText.Text = "<div style='background-color:#F5D0D9; border:solid 1px #000000; width:200px; text-align:center;'>÷ééîåú øùåîåú úçæå÷ä, ÷ùåøåú ìùëáä äæå.<br/>ìà ðéúï ìîçå÷ àú äùëáä.</div>";
                    }
				    break;
			    case MarkupCommand.Refresh:
				    break;
			    case MarkupCommand.Close:
				    markupManager.CloseMarkup();
				    refreshMap = true;
				    break;
            }
        }
	    availableMarkup = markupManager.GetAvailableMarkup();
	    openMarkup = markupManager.GetOpenMarkup();
		
	    // Remove open markup from the list of available markup.
	    foreach(String key in openMarkup.Keys)
	    {
	        if(availableMarkup.ContainsKey(key))
	        {
	            availableMarkup.Remove(key);
	        }
	    }
    }
    catch (MgException mge)
    {
        errorMsg = mge.GetExceptionMessage();
        errorDetail = mge.GetDetails();
    }
    catch(Exception e)
    {
        errorMsg = e.Message;
    }
%>

<html>
<head>
	<title>Manage Markups</title>
    <link rel="stylesheet" href="../../styles/gt.css" type="text/css">
	
	<script language="javascript">
        var session = '<%= GetParameter(args, "SESSION") %>';
        var mapName = '<%= GetParameter(args, "MAPNAME") %>';
	
		var CMD_NEW 	= <%= MarkupCommand.Create %>;
		var CMD_OPEN	= <%= MarkupCommand.Open %>;
		var CMD_DELETE	= <%= MarkupCommand.Delete %>;
		var CMD_REFRESH	= <%= MarkupCommand.Refresh %>;
		var CMD_EDIT	= <%= MarkupCommand.Edit %>;
		var CMD_CLOSE	= <%= MarkupCommand.Close %>;
			
		function SubmitCommand(cmd)
		{
            var commandInput = document.getElementById("commandInput");
			commandInput.value = cmd;

            var markupForm = document.getElementById("markupForm");
			if (cmd == CMD_NEW)
				markupForm.action = "newmarkup.aspx";
			else if (cmd == CMD_EDIT)
				markupForm.action = "editmarkup.aspx";
			else
				markupForm.action = "markupmain.aspx";
		    if(cmd == CMD_DELETE)
		    {
		        if(!confirm("äùéëáä úîç÷. ìäîùéê?")) return;
		    }
			markupForm.submit();
		}
		
		function OnAvailableMarkupChange()
		{
            var availableSelect = document.getElementById("availableMarkup");
			var openBtn = document.getElementById("openBtn");
			var deleteBtn = document.getElementById("deleteBtn");
			
			if (availableSelect.selectedIndex >= 0)
			{
				openBtn.disabled = false;
				deleteBtn.disabled = false;
			}
			else
			{
				openBtn.disabled = true;
				deleteBtn.disabled = true;
			}
		} 

		function OnOpenMarkupChange()
		{
            var openSelect = document.getElementById("openMarkup");
			var editBtn = document.getElementById("editBtn");
			var closeBtn = document.getElementById("closeBtn");
			
			if (openSelect.selectedIndex >= 0)
			{
				editBtn.disabled = false;
				closeBtn.disabled = false;
			}
			else
			{
				editBtn.disabled = true;
				closeBtn.disabled = true;
			}
		} 
		
		function OnLoad()
		{
			OnAvailableMarkupChange();
			OnOpenMarkupChange();
		
		<% if (refreshMap) { %>
			parent.parent.Refresh();
		<% } %>
		}
	</script>
	
</head>

<body dir=rtl onLoad="OnLoad()" style="margin:5px">

<% if (errorMsg == null) { %>

<form action="" method="post" enctype="application/x-www-form-urlencoded" id="markupForm" target="_self">
<table class="RegText" border="0" cellspacing="0" width="100%">
	<tr><td class="Title">ניהול שכבות<hr></td></tr>
	<tr><td class="SubTitle">רשימת שכבות נוכחית</td></tr>
	<tr>
		<td class="RegText">
			<select dir=rtl name="MARKUPLAYER" size="15" class="Ctrl" id="availableMarkup" onChange="OnAvailableMarkupChange()" style="width: 100%">
				<%
                    String selected = "selected";
                    foreach (String markupResId in availableMarkup.Keys)
                    {
                        String markupName = (String)availableMarkup[markupResId];
				%>
				<option value="<%= markupResId %>" <%=selected %> ><%= markupName%></option> 
				<%
                        selected = "";
                    }
				%>
		  	</select>
		</td>
	</tr>
	<tr>
		<td>
		    <asp:Literal ID="UnSuccessText" runat="server" Visible="false"></asp:Literal>
			<input class="Ctrl" type="button" id="newBtn" onClick="SubmitCommand(CMD_NEW)" value="חדש" style="width:40px">
			<input class="Ctrl" type="button" id="openBtn" onClick="SubmitCommand(CMD_OPEN)" value="הצג" style="width:40px">
			<input class="Ctrl" type="button" id="deleteBtn" onClick="SubmitCommand(CMD_DELETE)" value="מחק" style="width:40px">
			<input class="Ctrl" type="button" id="refreshBtn" onClick="SubmitCommand(CMD_REFRESH)" value="רענן" style="width:40px">
			<br><br>
		</td>
	</tr>
	<tr><td class="SubTitle">רשימת שכבות מוצגת</td></tr>
	<tr>
		<td class="RegText">
			<select name="OPENMARKUP" size="10" class="Ctrl" id="openMarkup" onChange="OnOpenMarkupChange()" style="width: 100%">
				<%
                    selected = "selected";
                    foreach (String markupLayer in openMarkup.Keys)
                    {
                        String markupName = (String)openMarkup[markupLayer];
				%>
				<option value="<%= markupLayer %>" <%=selected %> ><%= markupName%></option> 
				<%
                        selected = "";
                    }
				%>
		  	</select>
		</td>
	</tr>
	<tr>
		<td>
			<input class="Ctrl" type="button" id="editBtn" onClick="SubmitCommand(CMD_EDIT)" value="עדכן" style="width:40px">
			<input class="Ctrl" type="button" id="closeBtn" onClick="SubmitCommand(CMD_CLOSE)" value="סגור" style="width:40px">
			<br><br>
		</td>
	</tr>	
</table>
<input name="SESSION" type="hidden" value="<%= GetParameter(args, "SESSION") %>">
<input name="MAPNAME" type="hidden" value="<%= GetParameter(args, "MAPNAME") %>">
<input name="LAYOUT" type="hidden" value="<%= GetParameter(args, "LAYOUT") %>">
<input name="MARKUPCOMMAND" type="hidden" value="" id="commandInput">
</form>

<% } else { %>

<table class="RegText" border="0" cellspacing="0" width="100%%">
	<tr><td class="Title">Error<hr></td></tr>
	<tr><td><%= errorMsg %></td></tr>
	<tr><td><%= errorDetail %></td></tr>
</table>

<% } %>

</body>

</html>
